using System;
using Microsoft.Xna.Framework;

namespace SplashOn_PhoneLib
{
    public class CalculationHelper
    {
        public static float CalculateAngleDegreesFromUnitY(Vector3 from, Vector3 to)
        {
            return CalculateAngleDegreesFromUnitY(
                new Vector2(from.X, from.Y),
                new Vector2(to.X, to.Y)
                );
        }

        public static float CalculateAngleDegreesFromUnitY(Vector2 from, Vector2 to)
        {
            double hyp = Math.Sqrt( 
                            Math.Pow((double)(to.X - from.X), 2) + 
                            Math.Pow((double)(to.Y - from.Y), 2)
                            );
            double opp = to.X - from.X;
            double adj = to.Y - from.Y;
            float cos = MathHelper.ToDegrees((float)Math.Acos(adj / hyp));
            float sin = MathHelper.ToDegrees((float)Math.Asin(opp / hyp));
            //DebugHelper.w("from=" + from.ToString() + ", to=" + to.ToString() 
            //    + ":: opp=" + opp + ", adj=" + adj + ", hyp=" + hyp);
            //DebugHelper.w("sin=" + sin + ", cos=" + cos);
            if (opp < 0 && hyp < 0)
                return 360 + sin;
            if(opp < 0)
                return 360-cos;
            return cos;
        }

        public static Vector2 CalculateDistanceVector2(Vector2 tapPoint)
        {
            DebugHelper.w("tappoint = " + tapPoint.ToString());
            float x = (tapPoint.X - AppConstants.GROUND_MARKER_X_DIM);
            float y = (tapPoint.Y - AppConstants.GROUND_MARKER_Y_DIM);
            float divx = (float)((float)100 / (float)180);
            float divy = (float)((float)100 / (float)140);
            DebugHelper.w("divx = " + divx + ", divy = " + divy);
            DebugHelper.w("x = " + x + ", y = " + y);
            Vector2 pointOnMarker = Vector2.Zero;
            pointOnMarker.X = (int)((float)(x * divx));
            pointOnMarker.Y = -(int)((float)(y * divy));
            DebugHelper.w("pointOnMarker = " + pointOnMarker.ToString());
            return pointOnMarker;
        }

        public static Vector3 CalculateDistanceVector3(Vector2 tapPoint)
        {
            Vector2 pointOnMarker = CalculateDistanceVector2(tapPoint);
            DebugHelper.w(tapPoint.Y.ToString() + ", " + pointOnMarker.ToString());
            return new Vector3(pointOnMarker.X, pointOnMarker.Y, AppConstants.DEFAULT_Z_VALUE);
        }

    }
}
